<template>
  <div class="pages brandbrandClass" id="app">
    <!-- 弹窗 -->
    <!-- 操作框 -->
    <div class="tableSearch">
      <el-form label-width="100px" class="demo-ruleForm">
        <el-form-item label="" label-width="15px">
          <el-input class="input" v-model="selectdata.orgKey" size="small" placeholder="请输入商户名称"></el-input>
        </el-form-item>
        <el-form-item label="" label-width="15px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelectOrg" class="Button">查询</el-button>
        </el-form-item>
        <el-form-item label="" label-width="15px">
          <el-button type="primary" @click="cancel()" size="mini" v-if="unallocates"> 批量取消分配</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table :data="shopList" ref="multipleTable" max-height="300" @selection-change="changeFun">
      <el-table-column type="selection" width="55" @selection-change="changeFun"></el-table-column>
      <el-table-column type="index" label="序号" width="50"></el-table-column>
      <el-table-column property="name" label="门店名称" align="center"></el-table-column>
      <el-table-column property="shopCode" label="编号" align="center"></el-table-column>
      <el-table-column v-if="unallocates" prop="name" align="center" label="操作" width="280">
        <template slot-scope="scope">
          <el-button size="small" @click="cancel()" type="text" class="addDetail">取消分配</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageblock">
      <el-button size="small" class="topage" type="primary" @click="toPages">确定</el-button>
      <el-pagination @size-change="handleSizeChanges" @current-change="handleCurrentChanges" :current-page="currentPages" :page-sizes="[10,20,30,50]" :page-size="pageSizes" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <div class="operationBox StoreBranch">
      <el-button type="primary" @click="distribution" size="medium" v-if="!unallocates" style="margin-right:10px;">立即分配</el-button>
      <el-button plain @click="Sub" size="medium">返回</el-button>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { selectBaseShopPageList,toLowerShop } from "@/api";
import util from "@/util";
export default {
  computed: mapState(["merchantId", "sub", "power"]),
  props: {
    dialogVisible: "",
    unallocate: "",
    isShow:"",
    integralMulti: [],
    // {
    //   type: Array,
    //   default: [],
    //   required: true
    // }
  },
  data() {
    return {
      deployId: "",
      unallocates: this.unallocate,
      dialogVisibles: this.dialogVisible,
      dis: false,
      selectdata: {
        keyword: ""
      },
      shopList: [],
      integralMultis: this.integralMulti,
      // 分页开始
      total: 0,
      pageSizes: 20,
      totalItemss: 0,
      currentPages: 1,
      ids: ""
    };
  },
  watch: {
    unallocate: function(val) {
      this.unallocates = val;
    },
    integralMulti: function(val) {
      console.log(val);
      this.integralMultis = val;
    }
  },
  created() {
    // 获取品牌分类名称列表
  },
  mounted() {
    // 获取表格数据
    this.tableget_org(20, 1);
    console.log("isShow:",this.unallocate)
  },
  methods: {
    cancel() {
      this.$confirm("确定要取消对本门店的分配吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
        // type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    Sub() {
      console.log("123:",12345678)
      this.$emit("dialogVis", false);
    },
    //分配商户多选
    changeFun(val) {
      this.isshop = true;
      this.shopMulti = val;
    },
    //分配商户
    tableget_org(pageSize, pageNum) {
      let Data = {
        pageNum: pageNum,
        pageSize: pageSize,
        businessId: this.merchantId,
        status: 0,
        nameOrCode: this.selectdata.orgKey,
        type: "4"
      };
      selectBaseShopPageList(Data).then(res => {
        this.total = res.data.pageTotal;
        this.shopList = res.data.list;
        let aress = [];
      });
    },
    searchSelectOrg() {
      //门店查询
      this.tableget_org(this.pageSizes, 1);
    },
    distribution() {
      var arr = [];
      var arrs = [];
      console.log(this.integralMulti)
      for (var i = 0; i < this.integralMulti.length; i++) {
        arr.push(this.integralMulti[i].id);
      }
      if (this.shopMulti === undefined) {
        this.$message({
          type: "info",
          message: "请选择商户"
        });
        return false;
      }
      for (var i = 0; i < this.shopMulti.length; i++) {
        arrs.push(this.shopMulti[i].id);
      }
      console.log(arr)
      let data = {
        goodsIds: arrs.toString(),
        shopIds: arr.toString()
      };
      toLowerShop(data).then(res => {
        this.$message({
          type: "success",
          message: "分配成功!"
        });
        this.dialogVisibles = false;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //分配下级组织机构-----------分页
    toPages() {
      this.tableget_org(this.pageSizes, this.currentPages);
    },
    handleSizeChanges(val) {
      this.pageSizes = val;
      this.tableget_org(this.pageSizes, this.currentPages);
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChanges(val) {
      this.currentPages = val;
      this.tableget_org(this.pageSizes, this.currentPages);
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
#app {
  text-align: left;
}
.StoreBranch {
  width: 200px;
  margin: 10px auto;
  margin-top: 20px;
}
</style>

